package com.ruoyi.report_tg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_tg.domain.bo.TdczbjlbeBaDetailBo;
import com.ruoyi.report_tg.domain.vo.TdczbjlbeBaDetailVo;
import com.ruoyi.report_tg.domain.TdczbjlbeBaDetail;
import com.ruoyi.report_tg.mapper.TdczbjlbeBaDetailMapper;
import com.ruoyi.report_tg.service.ITdczbjlbeBaDetailService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 土的承载比二 详细Service业务层处理
 *
 * @author zyb
 * @date 2024-05-24
 */
@RequiredArgsConstructor
@Service
@Transactional
public class TdczbjlbeBaDetailServiceImpl implements ITdczbjlbeBaDetailService {

    private final TdczbjlbeBaDetailMapper baseMapper;

    /**
     * 查询土的承载比二 详细
     */
    @Override
    public TdczbjlbeBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询土的承载比二 详细列表
     */
    @Override
    public TableDataInfo<TdczbjlbeBaDetailVo> queryPageList(TdczbjlbeBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TdczbjlbeBaDetail> lqw = buildQueryWrapper(bo);
        Page<TdczbjlbeBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询土的承载比二 详细列表
     */
    @Override
    public List<TdczbjlbeBaDetailVo> queryList(TdczbjlbeBaDetailBo bo) {
        LambdaQueryWrapper<TdczbjlbeBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TdczbjlbeBaDetail> buildQueryWrapper(TdczbjlbeBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TdczbjlbeBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, TdczbjlbeBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getCol() != null, TdczbjlbeBaDetail::getCol, bo.getCol());
        lqw.eq(bo.getRow() != null, TdczbjlbeBaDetail::getRow, bo.getRow());
        lqw.eq(bo.getLiz() != null, TdczbjlbeBaDetail::getLiz, bo.getLiz());
        lqw.eq(bo.getDanwyl() != null, TdczbjlbeBaDetail::getDanwyl, bo.getDanwyl());
        lqw.eq(bo.getGuanrlds1() != null, TdczbjlbeBaDetail::getGuanrlds1, bo.getGuanrlds1());
        lqw.eq(bo.getGuanrlds2() != null, TdczbjlbeBaDetail::getGuanrlds2, bo.getGuanrlds2());
        lqw.eq(bo.getGuanrl() != null, TdczbjlbeBaDetail::getGuanrl, bo.getGuanrl());
        return lqw;
    }

    /**
     * 新增土的承载比二 详细
     */
    @Override
    public Boolean insertByBo(TdczbjlbeBaDetailBo bo) {
        TdczbjlbeBaDetail add = BeanUtil.toBean(bo, TdczbjlbeBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改土的承载比二 详细
     */
    @Override
    public Boolean updateByBo(TdczbjlbeBaDetailBo bo) {
        TdczbjlbeBaDetail update = BeanUtil.toBean(bo, TdczbjlbeBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TdczbjlbeBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除土的承载比二 详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
